/**
 * @Title: RuleController.java
 * @Package com.lemon.controller.cityRun
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年10月16日
 * @version V1.0
 */
package com.lemon.controller.cityRun;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.model.cityRun.CrSysRule;
import com.lemon.model.cityRun.CrSysRuleGroup;
import com.lemon.service.cityRun.CrSysRuleService;
import com.lemon.utils.ReqResult;

/**
 * <p>
 * Title: RuleController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年10月16日
 * @版本 V 1.0
 */
@RestController
@RequestMapping("/rule")
public class RuleController {


	@Resource
	private CrSysRuleService ruleService;


	//获取全部用户信息
	@RequestMapping("/getRuleList")
	public ReqResult getRuleList(@ModelAttribute CrSysRule rule , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			rule.setOrderBy(" order by create_time desc");
			List<CrSysRule> list = ruleService.selectListBySelective(rule);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	//获取全部用户信息
	@RequestMapping("/getRuleById")
	public ReqResult getRuleById(@ModelAttribute CrSysRule rule , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			CrSysRule list = ruleService.selectByPrimaryKey(rule.getId());
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 添加权限信息
	 * </blockquote></b>
	 * 
	 * @Title: addRuleInfo
	 * @param rule
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月16日
	 */
	@RequestMapping(value = "/addRuleInfo.do" , method = RequestMethod.POST)
	public ReqResult addRuleInfo(@ModelAttribute CrSysRule rule , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			rule.setStatus(1);
			int row = ruleService.insertSelective(rule);
			if (row > 0) {
				reqResult.setReturnResult(row);
			} else {
				reqResult.setReturnResult(row , ReqResult.code_fail);
			}
		} catch (Exception e) {
			reqResult.setReturnResult("添加权限异常" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 添加权限信息
	 * </blockquote></b>
	 * 
	 * @Title: addRuleInfo
	 * @param rule
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月16日
	 */
	@RequestMapping(value = "/updateRuleInfo.do" , method = RequestMethod.POST)
	public ReqResult updateRuleInfo(@ModelAttribute CrSysRule rule , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			int row = ruleService.updateByPrimaryKeySelective(rule);
			if (row > 0) {
				reqResult.setReturnResult(row);
			} else {
				reqResult.setReturnResult(row , ReqResult.code_fail);
			}
		} catch (Exception e) {
			reqResult.setReturnResult("添加权限异常" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	////////////////////////////////////////////////////权限组/////////////////////////////////////////////////
	/**
	 * <b>Description:<blockquote> 添加权限信息
	 * </blockquote></b>
	 * 
	 * @Title: addRuleInfo
	 * @param rule
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月16日
	 */
	@RequestMapping(value = "/addRuleGroup.do" , method = RequestMethod.POST)
	public ReqResult addRuleGroup(@ModelAttribute CrSysRuleGroup group , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			group.setStatus(1);
			int row = ruleService.insertGroupSelective(group);
			if (row > 0) {
				reqResult.setReturnResult(row);
			} else {
				reqResult.setReturnResult(row , ReqResult.code_fail);
			}
		} catch (Exception e) {
			reqResult.setReturnResult("添加权限异常" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	//获取全部组信息
	@RequestMapping("/getRuleGroupList")
	public ReqResult getRuleGroupList(@ModelAttribute CrSysRuleGroup group , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			group.setOrderBy(" order by create_time desc");
			group.setStatus(1);
			List<CrSysRuleGroup> list = ruleService.selectGroupListBySelective(group);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 添加权限信息
	 * </blockquote></b>
	 * 
	 * @Title: addRuleInfo
	 * @param rule
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月16日
	 */
	@RequestMapping(value = "/updateRuleGroupInfo.do" , method = RequestMethod.POST)
	public ReqResult updateRuleGroupInfo(@ModelAttribute CrSysRuleGroup group , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			int row = ruleService.updateGroupByPrimaryKeySelective(group);
			if (row > 0) {
				reqResult.setReturnResult(row);
			} else {
				reqResult.setReturnResult(row , ReqResult.code_fail);
			}
		} catch (Exception e) {
			reqResult.setReturnResult("添加权限异常" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	//根据id查询组信息
	@RequestMapping("/getRuleGroupById")
	public ReqResult getRuleGroupById(@ModelAttribute CrSysRuleGroup group , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			CrSysRuleGroup list = ruleService.selectGroupByPrimaryKey(group.getId());
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}
	////////////////////////////////////////////////////权限组end/////////////////////////////////////////////////
}
